<?
    include "../common.php" ;
    $opt = $_POST['opt'] ; 
    $arr = [] ;
    if ( $opt == 'getCategoryList' ){
        $sql = "SELECT * FROM `type` WHERE subtypeid IS NULL LIMIT 0 , 16" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ;
        }
    }
    if( $opt == 'getTransImg' ){
        $sql = "SELECT * FROM `transimg` WHERE isTrue = 1 AND isDelete = 1" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ;
        }
    }
    if( $opt == 'getProList' ){
        $temperId = $_POST['temperId'] ; 
        $temperNum = $_POST['temperNum'] ; 
        $num = floor($temperNum/5) * 5 ;
        $sql2 = "SELECT * FROM products WHERE temperId = {$temperId} ORDER BY id DESC LIMIT 0,$num";
        $res2 = mysqli_query($link,$sql2);
        while( $one2 = mysqli_fetch_assoc($res2) ){
            $arr[] = $one2 ; 
        }
    }
    if( $opt == 'getProDetail' ){
        $pid = $_POST['pid'] ; 
        $sql = "SELECT * FROM products WHERE id = {$pid}" ;
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one;
        }
    }
    if( $opt == 'getCarList' ){
        $uid = $_POST['uid'] ; 
        $page = $_POST['page'] ; 
        $perPage = $_POST['perPage'] ; 
        $pageSt = ( $page - 1 ) * $perPage ; 
        $sql = "SELECT pNum,shopTime,pName,pDescribe,price,showImg,typeName FROM carts AS c JOIN products AS p ON c.pId = p.id JOIN `type` AS t ON p.typeId = t.id WHERE uid =  $uid ORDER BY c.id ASC LIMIT {$pageSt},{$perPage}" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ; 
        }
    }
    if( $opt == 'getUPage' ){
        $uid = $_POST['uid'] ;
        $perPage = $_POST['perPage'] ;
        $sql = "SELECT COUNT(*) AS c FROM carts AS c JOIN products AS p ON c.pId = p.id JOIN `type` AS t ON p.typeId = t.id WHERE uid =  $uid" ;
        $res = mysqli_query($link,$sql);
        $one = mysqli_fetch_assoc($res);
        $arr['totalPage'] = ceil( $one['c'] / $perPage ) ; 
    }
    if( $opt == 'getTypeList' ){
        $typeId = $_POST['typeId'] ;
        $page = $_POST['page'] ; 
        $perPage = $_POST['perPage'] ; 
        $sql = "SELECT p.id AS id,pName,pDescribe,price,showImg,typeName FROM products AS p JOIN `type` AS t ON p.typeId = t.id WHERE p.typeId = {$typeId} or t.subtypeid = {$typeId}";
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ; 
        }
    }
    if( $opt == 'getTempList' ){
        $sql = "SELECT * FROM (SELECT temperId,temperName,color,tDescribe,COUNT(*) AS c FROM `products` AS p JOIN temper AS t ON t.id = p.temperId WHERE t.is_show = 1 GROUP BY temperId ) Tab WHERE c >= 5" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ; 
        }
    }
    echo json_encode($arr);
?>